/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg;

import nanoxml.XMLElement;

/**
 * This is a message class which goes to a particular game such as chess,
 * checkers etc.  The extending class doesn't have to set the game - this is
 * done behind the scenes.  The gameID should be the game and version
 * seperated with a colon e.g. chess:0.1
 *
 * @author  Bob Marks
 * @version Beta 0.3
 */
public abstract class MsgGameMessage implements ITransmittable {

    private static final int NO_STATUS = -1;

    /** Integer status of this comm message. */
    protected int status;

    /**
     * Username of the person who sent this message.  This is usually
     * required to be set if a client needs to know who sent a message
     * and is set in the server.
     */
    protected long userid = -1L;

    /**
     * Username of the person who this is being sent to (if empty then no one).
     */
    protected long userIdTo = -1L;

    /** Type of message is used to determine which layer the message is for. */
    public static final String XML_ATT_STATUS = "status";
    public static final String XML_ATT_USERNAME = "userid";
    public static final String XML_ATT_USERNAME_TO = "useridTo";

    /**
     * Constructor with no parameters (no status).
     */
    protected MsgGameMessage() {
        this(NO_STATUS, NO_STATUS);
    }

    /**
     * Constructor which takes a username and a status.
     *
     * @param status    A status
     */
    protected MsgGameMessage(int status) {
        this(status, NO_STATUS);
    }

    /**
     * Constructor which only takes a username.
     *
     * @param usernameTo
     */
    public MsgGameMessage(long userid) {
        this(NO_STATUS, userid);
    }

    /**
     * Constructor which takes a status and a username to.
     *
     * @param status
     * @param usernameTo
     */
    public MsgGameMessage(int status, long userid) {
        this.status = status;
        this.userid = userid;
    }

    /**
     * Constructor which parses the XMLElement into class fields.
     *
     * @param message  Element to parse.
     */
    protected MsgGameMessage(XMLElement message) {
        this.status = message.getIntAttribute(XML_ATT_STATUS, NO_STATUS);
        this.userid = message.getLongAttribute(XML_ATT_USERNAME);
        this.userIdTo = message.getLongAttribute(XML_ATT_USERNAME_TO);
    }

    /**
     * Return the status.
     *
     * @return    Status of user.
     */
    public int getStatus() {
        return status;
    }

    /**
     * Set the optional uesrname variable.
     *
     * @param username
     */
    public void setUsername(long userid) {
        this.userid = userid;
    }

    /**
     * Return the optional usernameTo variable.
     *
     * @return  username
     */
    public long getUserid() {
        return userid;
    }

    /**
     * Set the optional usernameTo variable.
     *
     * @param usernameTo
     */
    public void setUsernameTo(long usernameTo) {
        this.userIdTo = usernameTo;
    }

    /**
     * Return the optional usernameTo variable.
     *
     * @return  username
     */
    public long getUsernameTo() {
        return userid;
    }

    /**
     * Return true / false on the username being set.
     *
     * @return   True if username is set.
     */
    public boolean isUsernameSet() {
        return (userid != -1L);
    }

    /**
     * Return true / false on the usernameTo being set.
     *
     * @return   True if usernameTo is set.
     */
    public boolean isUsernameToSet() {
        return (userIdTo != -1L);
    }

    /**
     * Flatten a DG message and indent it.
     *
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return flatten().toString(true);
    }

    /**
     * Create a generic (nameless) XMLElement which sub classes can use.
     *
     * @param name	Name of the XML element.
     * @return      XML version of this class.
     */
    public XMLElement flatten(String name) {
        XMLElement message = new XMLElement(name);

        if (status != NO_STATUS) {
            message.setIntAttribute(XML_ATT_STATUS, status);
        }
        if (isUsernameSet()) {
            message.setLongAttribute(XML_ATT_USERNAME, userid);
        }
        if (isUsernameToSet()) {
            message.setLongAttribute(XML_ATT_USERNAME_TO, userIdTo);
        }

        return message; // return element back to user
    }
}
